package com.uroad.lib.imageloader;

import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v4.app.FragmentActivity;
import android.widget.ImageView;

import com.bumptech.glide.Glide;

import jp.wasabeef.glide.transformations.CropCircleTransformation;

/**
 * Created by jiadong on 2016/11/2.
 * use this class to load image,single instance
 */

public class ImageLoaderHelper {

    public static final int PIC_QUALITY_RGB_565 = 1;//标清
    public static final int PIC_QUALITY_ARGB_8888 = 2;//高清
    public static final int PIC_QUALITY_NO_PIC = 3;//无图

    public static final int Transform_Defult = 3;
    public static final int Transform_CropCircle = 4;
    public static final int Transform_RoundedCorners = 5;

    private static ImageLoaderHelper mInstance;
    private BaseImageLoaderStrategy mStrategy;

    //图片质量，默认标清。考虑到兼容以前的项目，新增一个方法设置图片质量，没有设置的按默认质量（标清）
    private int quality = PIC_QUALITY_RGB_565;

    public void setQuality(int quality){
        this.quality = quality;
    }

    private ImageLoaderHelper() {
        mStrategy = new GlideImageLoaderStrategy();
    }

    //single instance
    public static ImageLoaderHelper getInstance() {
        if (mInstance == null) {
            synchronized (ImageLoaderHelper.class) {
                if (mInstance == null) {
                    mInstance = new ImageLoaderHelper();
                    return mInstance;
                }
            }
        }
        return mInstance;
    }

    public void loadImage(Context context, ImageLoader img) {
        mStrategy.loadImage(context, img);
    }

    public void loadImage(Context context, String url, ImageView imageView) {
        ImageLoader imageLoader = new ImageLoader.Builder().url(url)
                .imgView(imageView).quality(quality).needCache(true).build();
        mStrategy.loadImage(context, imageLoader);
    }

    public void loadImage(Context context, String url, ImageView imageView,int failed,int loading){
        ImageLoader imageLoader = new ImageLoader.Builder().url(url)
                .imgView(imageView).error(failed).placeHolder(loading).quality(quality).needCache(true).build();
        mStrategy.loadImage(context, imageLoader);
    }

    /**
     * 加载图标并显示成圆形
     *
     * @param context
     * @param url
     * @param imageView
     */
    public void loadCircleImage(Context context, String url, ImageView imageView) {
        ImageLoader imageLoader = new ImageLoader.Builder().url(url)
                .transform(ImageLoaderHelper.Transform_CropCircle, 0)
                .imgView(imageView).quality(quality).build();
        mStrategy.loadImage(context, imageLoader);
    }

    /**
     * 加载图标并显示成圆形
     *
     * @param context
     * @param url
     * @param imageView
     * @param defaultRes
     */
    public void loadCircleImage(Context context, String url, ImageView imageView , int defaultRes) {
        ImageLoader imageLoader = new ImageLoader.Builder().url(url)
                .transform(ImageLoaderHelper.Transform_CropCircle, 0)
                .imgView(imageView).quality(quality).error(defaultRes).build();
        mStrategy.loadImage(context, imageLoader);
    }

    /**
     * 加载图标并显示成圆形
     *
     * @param context
     * @param url
     * @param imageView
     */
    public void loadCircleImage(Context context, String url, ImageView imageView,int failed,int loading) {
        ImageLoader imageLoader = new ImageLoader.Builder().url(url)
                .transform(ImageLoaderHelper.Transform_CropCircle, 0)
                .imgView(imageView).error(failed).placeHolder(loading).quality(quality).build();
        mStrategy.loadImage(context, imageLoader);
    }

    /**
     * 加载图标并显示成圆角
     *
     * @param context
     * @param url
     * @param imageView
     * @param radius
     */
    public void loadRoundImage(Context context, String url, ImageView imageView, int radius) {
        ImageLoader imageLoader = new ImageLoader.Builder().url(url)
                .transform(ImageLoaderHelper.Transform_RoundedCorners, radius)
                .imgView(imageView).quality(quality).build();
        mStrategy.loadImage(context, imageLoader);
    }

    /**
     * 加载图片（不缓存）
     *
     * @param context
     * @param url
     * @param imageView
     */
    public void loadNoCacheImage(Context context, String url, ImageView imageView) {
        ImageLoader imageLoader = new ImageLoader.Builder().url(url)
                .imgView(imageView).needCache(false).quality(quality).build();
        mStrategy.loadImage(context, imageLoader);
    }

    public Bitmap loadImage(Context context, String url, int width, int height) {
        try {
            return Glide.with(context).load(url).asBitmap().into(width, height).get();
        } catch (Exception e) {
            return null;
        }
    }

    public Bitmap loadCircleImage(Context context, String url, int width, int height) {
        try {
            return Glide.with(context).load(url).asBitmap().transform(new CropCircleTransformation(context)).into(width, height).get();
        } catch (Exception e) {
            return null;
        }
    }

    public void loadImage(Activity activity, ImageLoader img) {
        mStrategy.loadImage(activity, img);
    }

    public void loadImage(FragmentActivity activity, ImageLoader img) {
        mStrategy.loadImage(activity, img);
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    public void loadImage(Fragment fragment, ImageLoader img) {
        mStrategy.loadImage(fragment.getContext(), img);
    }

    public void loadImage(android.support.v4.app.Fragment fragment, ImageLoader img) {
        mStrategy.loadImage(fragment.getContext(), img);
    }

    public void clearDiskCache(Context context) {
        mStrategy.clearDiskCache(context);
    }

    public void clearMemoryCache(Context context) {
        mStrategy.clearMemoryCache(context);
    }

    public void clearAllCache(Context context) {
        mStrategy.clearAllCache(context);
    }

    public String getCacheSize(Context context) {
        return mStrategy.getCacheSize(context);
    }

    public void setLoadImgStrategy(BaseImageLoaderStrategy strategy) {
        mStrategy = strategy;
    }
}
